package com.ys.controller.web.storage;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.storage.TccFbaBoxUp;
import com.ys.entity.storage.TccFbaBoxUpInfo;
import com.ys.entity.storage.TccFbaPlan;
import com.ys.entity.sys.YsMenu;
import com.ys.enume.storage.TccFbaBoxUpEnum;
import com.ys.enume.storage.TccFbaPlanEnum;
import com.ys.mapper.storage.TccFbaPlanMapper;
import com.ys.service.storage.TccFbaBoxUpService;
import com.ys.service.sys.CommonApiService;
import com.ys.util.DateUtil;
import com.ys.vo.W;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * FBA装箱单
 */
@Controller
@RequestMapping("/web/storage/tccFbaBoxUp")
public class WebTccFbaBoxUpController {
	private static final Logger logger = LoggerFactory.getLogger(WebTccFbaBoxUpController.class);


	@Autowired
	private CommonApiService<TccFbaBoxUp> commonApiService;

	@Autowired
	private TccFbaBoxUpService tccFbaBoxUpService;


	@Autowired
	private TccFbaPlanMapper tccFbaPlanMapper;

	private Long menuId = MenuId.TCC_FbaBoxUp;
	private Long menuIdFbaImportByBox = MenuId.TCC_FbaBoxUpFbaImportByBox;
	private Long menuId_updateFbaWarehouse = MenuId.TCC_FbaBoxUpFbaByUpdateFbaWarehouse;


	//箱规编辑
	@GetMapping("/boxRoleEdit")
	public String boxRoleEdit(Model model,String bid,String tableId,String isSearch){
		model.addAttribute("bid", bid);
		model.addAttribute("tableId", tableId);
		model.addAttribute("isSearch", isSearch);
		if(isSearch.equals("0")){
			return "storage/tccFbaBoxUp/boxRole";
		}else {
			return "storage/tccFbaBoxUp/boxRoleBySearch";
		}

	}


	//货件预览
	@GetMapping("/shipmentPreview")
	public String shipmentPreview(Model model,String bid,String tableId){
		try {
			int isEdit = 1;//是否可编辑 0否1是
			String choosePlacementOptionId = null;
			model.addAttribute("bid", bid);

			//model.addAttribute("isReCountPurchaseAmount", isReCountPurchaseAmount);
			model.addAttribute("tableId", tableId);
			TccFbaBoxUp tccFbaBoxUp = new TccFbaBoxUp().setBid(bid).queryFirst();
			if(tccFbaBoxUp.getPlacementOptionId()!=null && !"".equals(tccFbaBoxUp.getPlacementOptionId())){
				isEdit = 0;
				choosePlacementOptionId = tccFbaBoxUp.getPlacementOptionId();
			}
			int isNeedToApi = 0;//是否需要获取API
			TccFbaPlan tccFbaPlan = new TccFbaPlan().setBid(tccFbaBoxUp.getBidFbaPlan()).queryFirst();
			if(tccFbaPlan.getBoxRoleEditNo()!=0){
				if(tccFbaPlan.getBoxRoleEditNo() - tccFbaPlan.getGenerateNo()!=0 ||  tccFbaPlan.getApiGetStatus()==0 || tccFbaPlan.getApiGetStatus()==3){
					isNeedToApi = 1;
				}
			}
			int isNeedToWait = 0;//是否需要等待
			if(tccFbaPlan.getBoxRoleEditNo()!=0 && tccFbaPlan.getBoxRoleEditNo() - tccFbaPlan.getGenerateNo()==0 && tccFbaPlan.getApiGetStatus()==2){
				isNeedToWait = 1;
				//等待时间是否超过十分钟
				if(tccFbaPlan.getAuditingTime()==null || DateUtil.getMinutesSpace(tccFbaPlan.getApiGetTime(),new Date())-10>0){
					//修改状态为失败
					tccFbaPlan.setApiGetStatus(TccFbaPlanEnum.ApiGetStatus.FAIL.value);
					tccFbaPlanMapper.update(tccFbaPlan);
					isNeedToWait = 0;
				}
			}
//			if(isNeedToWait == 1){
//				return W.error(model,"系统正在获取方案中，请稍后");
//			}
			model.addAttribute("isNeedToApi", isNeedToApi);
			model.addAttribute("isNeedToWait", isNeedToWait);
			model.addAttribute("apiCount", tccFbaBoxUp.getApiCount());
			model.addAttribute("isEdit", isEdit);
			model.addAttribute("choosePlacementOptionId", choosePlacementOptionId);

			return "storage/tccFbaBoxUp/shipmentPreview";
		}catch (Exception e) {
			logger.error("页面出错：",e.getMessage(),e);
			return W.error(model, e.getMessage());
		}

	}


	//货件预览详情
	@GetMapping("/shipmentDetail")
	public String shipmentPreviewDetail(Model model, HttpServletRequest request, HttpServletResponse response, String bid, String shipmentId) {
		model.addAttribute("bid", bid);
		model.addAttribute("shipmentId",shipmentId);
		return "storage/tccFbaBoxUp/shipmentDetail";
	}



	//商品标、透明标、欧代标打印预览
	@GetMapping("/printTag")
	public String printTag(Model model,String bid,String printType){
		model.addAttribute("bid", bid);
		model.addAttribute("printType", printType);
		return "storage/tccFbaBoxUp/printTag";
	}



	/*
	 * 编辑 - FBA导入（按箱）
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response, String bid) {
		try {

			TccFbaBoxUp tccFbaBoxUp = new TccFbaBoxUp().setBid(bid).queryFirst();

			List<Map<String, Object>> itemList = tccFbaBoxUpService.getFbaImportByBoxItenList(bid);
			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(menuIdFbaImportByBox,itemList);
			if(tccFbaBoxUp.getBoxState().equals(TccFbaBoxUpEnum.BoxState.YES_PASS.value) ||
					tccFbaBoxUp.getBoxState().equals(TccFbaBoxUpEnum.BoxState.YES_CANCEL.value)){//已取消
				return commonApiService.toDetail(model,tccFbaBoxUp.getId(), new TccFbaBoxUp(), menuIdFbaImportByBox, null, subGridConfig);
			}
			return commonApiService.toEdit(model,tccFbaBoxUp.getId(), new TccFbaBoxUp(), menuIdFbaImportByBox, null, subGridConfig);

		}catch (Exception e) {
			logger.error("页面出错：",e.getMessage(),e);
			return W.error(model, e.getMessage());
		}
	}



	/*
	 * 修改FBA仓库
	 */
	@GetMapping("/updateFbaWarehouse")
	public String updateFbaWarehouse(Model model, HttpServletRequest request, HttpServletResponse response, String bid) {
		try {

			TccFbaBoxUp tccFbaBoxUp = new TccFbaBoxUp().setBid(bid).queryFirst();
			//验证是否进行过FBA导入
			int isImportFba = new TccFbaBoxUpInfo().setBoxUpBid(bid).count();
			if(isImportFba<=0){
				return W.error(model, "请先进行FBA导入");
			}

			List<Map<String, Object>> itemList = tccFbaBoxUpService.getFbaWarehouseItemList(bid);
			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(menuId_updateFbaWarehouse,itemList);

			List<TccFbaBoxUpEnum.BoxState> getEditInfo = TccFbaBoxUpEnum.BoxState.getEditInfo();
//			if(!TccFbaBoxUpEnum.BoxState.checkStatus(getEditInfo,tccFbaBoxUp.getBoxState())){
//				return commonApiService.toDetail(model,tccFbaBoxUp.getId(), new TccFbaBoxUp(), menuId_updateFbaWarehouse, null, subGridConfig);
//			}
			return commonApiService.toEdit(model,tccFbaBoxUp.getId(), new TccFbaBoxUp(), menuId_updateFbaWarehouse, null, subGridConfig);

		}catch (Exception e) {
			logger.error("页面出错：",e.getMessage(),e);
			return W.error(model, e.getMessage());
		}
	}


}






